<template>
  <div>
    <el-card class="box-card" shadow="always">
      <div slot="header" class="clearfix">
        <el-input
          v-model="$store.state.inputValue"
          placeholder="请输入内容"
          class="my_input"
          @change="handleInputChange"
        ></el-input>
        <el-button
          style="float: right; padding: 3px 0"
          type="primary"
          class="my_btn"
          @click="addTask"
          >添加任务</el-button
        >
      </div>
      <div v-for="item in infoList" :key="item.id" class="text item my_list">
        <el-checkbox
          @change="changeCheckboxStatus(item.id, item.done)"
          :checked="item.done"
          size="mini"
          >{{ item.info }}</el-checkbox
        >
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
          size="small"
          @click="removeItemById(item.id)"
          >删除</el-button
        >
      </div>
      <div class="footer">
        <span>{{ unDoneTask }}条剩余</span>
        <el-button-group>
          <el-button
            @click="changeList('all')"
            :type="taskType === 'all' ? 'primary' : 'default'"
            size="mini"
            >全部</el-button
          >
          <el-button
            @click="changeList('undone')"
            :type="taskType === 'undone' ? 'primary' : 'default'"
            size="mini"
            >未完成</el-button
          >
          <el-button
            @click="changeList('done')"
            :type="taskType === 'done' ? 'primary' : 'default'"
            size="mini"
            >已完成</el-button
          >
        </el-button-group>
        <el-button type="text" size="mini" @click="cleanDone"
          >清除已完成</el-button
        >
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  data() {
    return {}
  },
  created() {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['taskType']),
    ...mapGetters(['infoList', 'unDoneTask'])
  },
  methods: {
    // 监听文本框的内容变化
    handleInputChange(e) {
      // console.log(e)
      this.$store.commit('setInputValue', e)
    },
    // 添加任务
    addTask() {
      if (this.$store.state.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空!')
      }
      this.$store.commit('addItem')
    },
    // 根据id删除指定的任务
    removeItemById(id) {
      // console.log(id)
      this.$store.commit('removeItem', id)
    },
    // 监听复选框中状态变化的事件
    changeCheckboxStatus(id, status) {
      // console.log(id)
      // console.log(e)
      const param = {
        id: id,
        status: !status
      }
      this.$store.commit('changeStatus', param)
    },
    // 清除已经完成的任务
    cleanDone() {
      this.$store.commit('cleanItem')
    },
    // 修改页面上的任务状态 all done undone
    changeList(type) {
      // console.log(type)
      this.$store.commit('changeTaskType', type)
    }
  }
}
</script>

<style scoped>
.box-card {
  width: 600px;
  margin: 10px auto;
}
.clearfix {
  display: flex;
  margin-bottom: 10px;
}
.my_list {
  margin-bottom: 20px;
  text-align: left;
}
.my_input {
  flex: 5;
}
.my_btn {
  flex: 1;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  margin-top: 10px;
}
</style>
